package com.example.zhaohang.bingzhuangtu;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.zhaohang.adapter.SecondePagerAdpter;
import com.example.zhaohang.utils.DensityUtil;
import com.example.zhaohang.utils.RotatePageTransformer;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class SecondActivity extends BaseActivity {

    @InjectView(R.id.button_mian)
    Button       buttonMian;
    @InjectView(R.id.viewpager_select)
    ImageView    viewpagerSelect;
    @InjectView(R.id.viewpager_baidian)
    LinearLayout viewpagerBaidian;
    private ViewPager viewPager;
    private int[] imageResourID = new int[]{R.mipmap.guide_1, R.mipmap.guide_1, R.mipmap.guide_1};
    private int mPointWhith;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        ButterKnife.inject(this);
        //初始化view
        initView();
        //添加数据
        initData();
    }

    private void initData() {

        final List<ImageView> list = new ArrayList<>();
        list.clear();

        for (int i = 0; i < imageResourID.length; i++) {

            ImageView imageView = new ImageView(QQAppApolication.context);


            //1    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            //  imageView.setImageResource(imageResourID[i]);

            //2  这种不能用，这里针对的是布局管理，而不是具体的view
            // ViewPager.LayoutParams params = new ViewPager.LayoutParams(ViewPager.LayoutParams.FILL_PARENT, ViewPager.LayoutParams.FILL_PARENT);
            // imageView.setLayoutParams(params);

            imageView.setBackgroundResource(imageResourID[i]);

            list.add(imageView);


        }

        SecondePagerAdpter adpter = new SecondePagerAdpter(QQAppApolication.context, list);

        viewPager.setAdapter(adpter);

        //添加动画管理类
        viewPager.setPageTransformer(true, new RotatePageTransformer());

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            /**
             *
             * @param position  页面的索引
             * @param positionOffset  当前页面滑动的比例值
             * @param positionOffsetPixels  页面滑动的像素值
             */
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (position == list.size() - 1) {
                    //如果是就显示
                    buttonMian.setVisibility(View.VISIBLE);
                } else {
                    //如果不是就隐藏
                    buttonMian.setVisibility(View.INVISIBLE);
                }


                //当页面滑动时,小红点跟着滑动



                viewpagerSelect.setTranslationX((position+positionOffset)*mPointWhith);



            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        for (int i = 0; i < imageResourID.length; i++) {

            ImageView imageView = new ImageView(this);
            imageView.setImageResource(R.drawable.yuan);
            LinearLayout.LayoutParams params =
                    new LinearLayout.LayoutParams(DensityUtil.dip2px(SecondActivity.this, 15),
                            DensityUtil.dip2px(SecondActivity.this, 15));

            if (i!=0){
                params.leftMargin=(DensityUtil.dip2px(SecondActivity.this,20));
            }
            imageView.setLayoutParams(params);

            viewpagerBaidian.addView(imageView);
        }

        //获取两点之间的间距,这个一定要写在下面,不然回报空指针异常
        //等到控件完全显示后才去显示view
        viewPager.post(new Runnable() {

            @Override
            public void run() {
                mPointWhith = viewpagerBaidian.getChildAt(1).getLeft() - viewPager.getChildAt(0).getLeft();
            }
        });

    }
    //这里需要强调一下，根据viewpager的API，可以看书效果
    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.secondviewpager);
    }

    public void button_to_mian(View view) {

        StartActivity(LoginActivity.class, true);

    }
}
